﻿using Serilog;
using SqlSugar;
using WLYT.Application.Interfaces;
using WLYT.Domain.Entries;

namespace WLYT.WMS.Component
{
    public static class SqlSugarSetup
    {
        public static void AddSqlSugar(this IServiceCollection services, IConfiguration configuration)
        {
            List<ConnectionConfig> configConnection = new List<ConnectionConfig>
            {
                new ConnectionConfig()
                {
                    ConfigId = "db1",
                    DbType = DbType.SqlServer,
                    ConnectionString = configuration.GetConnectionString("wmsConnection"),
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                    MoreSettings = new ConnMoreSettings
                    {
                        IsWithNoLockQuery = true,
                        DisableWithNoLockWithTran = true
                    },
                    ConfigureExternalServices = new ConfigureExternalServices()
                    {
                        DataInfoCacheService = new SqlSugarRedisCache()
                    }
                },
                new ConnectionConfig()
                {
                    ConfigId = "db2",
                    DbType = DbType.SqlServer,
                    ConnectionString = configuration.GetConnectionString("logConnection"),
                    InitKeyType = InitKeyType.Attribute,
                    IsAutoCloseConnection = true,
                    MoreSettings = new ConnMoreSettings
                    {
                        IsWithNoLockQuery = true,
                        DisableWithNoLockWithTran = true
                    }
                }
            };
            services.AddSingleton<ISqlSugarClient>(new SqlSugarScope(configConnection, db =>
            {
            //    db.GetConnectionScope("db1").Aop.OnLogExecuted = (sql, pars) =>
            //    {
            //        Log.Information(db.Ado.CommandType == System.Data.CommandType.StoredProcedure ? UtilMethods.GetNativeSql(sql, pars) : UtilMethods.GetSqlString(DbType.SqlServer, sql, pars));
            //    };
            }));
        }
    }
}
